<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hondacustomermodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'customer_status' => 'customer_status',
        'sex' => 'sex',
        'customer_time_create_start' => 'customer_time_create >=',
        'customer_time_create_end' => 'customer_time_create <=',
        'customer_time_lottery_start' => 'customer_time_lottery >=',
        'customer_time_lottery_end' => 'customer_time_lottery <=',
        'openid' => 'openid',
        'gift_type' => 'honda_customer.gift_type',
        'gift_status' => 'honda_customer.gift_status',
        'customer_id' => 'honda_customer.customer_id',
        'customer_reservation_type' => 'honda_customer.customer_reservation_type',
    );

    public static $likeField = array(
        'nickname' => 'nickname',
        'province' => 'province',
        'city' => 'city',
        'customer_phone' => 'customer_phone',
        'customer_name' => 'customer_name',
    );
    
    public static $sortField = array(
        'lottery_times' => 'lottery_times',
        'money' => 'money',
        'gift_type' => 'honda_customer.gift_type',
    );

    public static $historySearchField = array(
        'customer_id' => 'honda_lottery_history.customer_id',
        'gift_type' => 'honda_lottery_history.gift_type',
        'gift_status' => 'honda_lottery_history.gift_status',
        'lottery_time_time_create_start' => 'lottery_time_time_create >=',
        'lottery_time_time_create_end' => 'lottery_time_time_create <=',
        'lottery_time_time_active_start' => 'lottery_time_time_active >=',
        'lottery_time_time_active_end' => 'lottery_time_time_active <=',
    );

    public static $historyLikeField = array(
        'nickname' => 'nickname',
        'customer_phone' => 'customer_phone',
        'customer_name' => 'customer_name',
    );

    public static $cardSearchField = array(
        'customer_id' => 'honda_card.customer_id',
        'card_type' => 'honda_card.card_type',
        'card_number' => 'honda_card.card_number',
        'card_status' => 'honda_card.card_status',
        'card_time_create_start' => 'card_time_create >=',
        'card_time_create_end' => 'card_time_create <=',
        'card_time_active_start' => 'card_time_active >=',
        'card_time_active_end' => 'card_time_active <=',
    );

    public static $cardLikeField = array(
        'nickname' => 'nickname',
    );

    public static $codeSearchField = array(
        'customer_id' => 'honda_code.customer_id',
        'code_type' => 'honda_code.code_type',
        'code_number' => 'honda_code.code_number',
        'code_code' => 'honda_code.code_code',
        'code_status' => 'honda_code.code_status',
    );

    public static $codeLikeField = array(
        'nickname' => 'nickname',
    );

    public static $recordSearchField = array(
        'customer_id' => 'honda_card.customer_id',
        'card_type' => 'honda_card.card_type',
        'card_number' => 'honda_card.card_number',
        'openid' => 'openid',
        'gift_type' => 'honda_customer.gift_type',
        'gift_status' => 'gift_status',
        'user_code' => 'user_code',
        'record_time_create_start' => 'record_time_create >=',
        'record_time_create_end' => 'record_time_create <=',
    );

    public static $recordLikeField = array(
        'record_name' => 'record_name',
        'record_phone' => 'record_phone',
        'user_name' => 'user_name',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getCustomerListSection($assist){
        if(!empty($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('honda_customer');
        $this->db->join('user','honda_customer.shop_id = user.user_id','left');
        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_sql'])) $this->db->where($assist['where_sql'],NULL,FALSE);
        if(!empty($assist['where_in'])){
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    return array(array(),0);
                }
            }
        }
        if(!empty($assist['pagination'])) {
            $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);
        }

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('honda_customer.customer_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) {
            $count = count($list);
        }

        return array($list,$count);
    }


    public function getCustomerById($customer_id){
        if(!is_array($customer_id)) $customer_id = array($customer_id);
        if(empty($customer_id)) return array();

        $this->db->from('honda_customer');
        $this->db->where_in('honda_customer.customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }


    public function getCustomer($customer_id){
        $this->db->from('honda_customer');
        $this->db->where('honda_customer.customer_id',$customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }


    public function getCustomerByOpenid($openid,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('honda_customer');
        $this->db->where('openid',$openid);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    public function getCustomerByPhone($customer_phone,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('honda_customer');
        $this->db->where('customer_phone',$customer_phone);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    public function getCustomerByRelonlineCustomerId($relonline_customer_id,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('honda_customer');
        $this->db->where('relonline_customer_id',$relonline_customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    

    public function getLotteryHistoryListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('honda_lottery_history');
        if(!isset($assist['nojoin'])) {
            $this->db->join('honda_customer','honda_customer.customer_id = honda_lottery_history.customer_id','left');
        }

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

		if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('lottery_history_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    public function getCardListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('honda_card');
        $this->db->join('honda_customer','honda_customer.customer_id = honda_card.customer_id','left');

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

        if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('card_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    public function getRecordListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('honda_val_record');
        $this->db->join('honda_card','honda_card.card_id = honda_val_record.card_id','left');
        $this->db->join('honda_customer','honda_customer.customer_id = honda_card.customer_id','left');
        $this->db->join('user','user.user_id = honda_val_record.user_id','left');

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

        if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('record_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    

    public function getCodeListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('honda_code');
        $this->db->join('honda_customer','honda_customer.customer_id = honda_code.customer_id','left');

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

        if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('code_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    public function getCardListForCustomerId($customer_id) {
        $this->db->from('honda_card');
        $this->db->where('customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getCardList($customer_ids = false) {
        if(is_array($customer_ids) && empty($customer_ids)) {
            return array();
        }
        $this->db->from('honda_card');
        if($customer_ids !== false) {
            $this->db->where_in('customer_id',$customer_ids);
        }
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getCardForCustomer($customer_id,$card_type) {
        $this->db->from('honda_card');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('card_type',$card_type);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getCard($card_id) {
        $this->db->from('honda_card');
        $this->db->where('card_id',$card_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getCardByNumber($card_number) {
        $this->db->from('honda_card');
        $this->db->where('card_number',$card_number);
        $this->db->join('honda_customer','honda_customer.customer_id = honda_card.customer_id','left');
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getAvailabelCode($code_type) {
        $this->db->from('honda_code');
        $this->db->where('code_status',STATUS_DISABLE);
        $this->db->where('code_type',$code_type);
        $this->db->limit(1);

        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getCodeForCustomer($customer_id) {
        $this->db->from('honda_code');
        $this->db->where('code_status',STATUS_ACTIVE);
        $this->db->where('customer_id',$customer_id);
        $this->db->limit(1);

        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }

    public function getLotteryHistoryGift($customer_id){
        $this->db->from('honda_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('gift_type >',0);
        $query = $this->db->get();
        $result = $query->row_array();

        return $result;
    }


    public function getLastLotteryTime($customer_id) {
        $this->db->from('honda_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->order_by('lottery_history_id','DESC');
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();
        $time = 0;
        if(!empty($result)) {
            $time = strtotime($result['lottery_time_time_lastmodified']);
        }

        return $time;
    }

    public function getAvailableLotteryTime($customer_id) {
        $this->db->from('honda_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $count = $this->db->count_all_results();

        $this->db->from('honda_card');
        $this->db->where('customer_id',$customer_id);
        $countCard = $this->db->count_all_results();

        return $countCard - $count;
    }
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createCustomer($info){
        $db_debug = $this->db->db_debug;
        // $this->db->db_debug = FALSE; 
        $m = $this->db->insert('honda_customer',$info);
        $customer_id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $customer_id : 0;
    }

    public function createLotteryHistory($info) {
        $this->db->insert('honda_lottery_history',$info);
        $id = $this->db->insert_id();
        return $id;
    }

    public function createCard($info) {
        $this->db->insert('honda_card',$info);
        $id = $this->db->insert_id();
        return $id;
    }

    public function createCode($info) {
        $this->db->insert('honda_code',$info);
        $id = $this->db->insert_id();
        return $id;
    }

    public function createCodeBatch($info){
        if (!empty($info)) {
            $this->db->insert_batch('honda_code',$info);
        }
    }

    public function createValRecord($info) {
        $this->db->insert('honda_val_record',$info);
        $id = $this->db->insert_id();
        return $id;
    }
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info) {
        $this->db->where('customer_id',$info['customer_id']);
        unset($info['customer_id']);
        $this->db->update('honda_customer',$info);
    }

    public function updateCustomerLotteryCount($customer_id) {
        $this->db->query('update {PRE}honda_customer a set lottery_times=(select count(*) 
                from {PRE}honda_lottery_history where customer_id=a.customer_id) WHERE a.customer_id='.$customer_id);
    }

    public function editLotteryHistory($info) {
        $this->db->where('lottery_history_id',$info['lottery_history_id']);
        unset($info['lottery_history_id']);
        $this->db->update('honda_lottery_history',$info);
    }

    public function editCard($info) {
        $this->db->where('card_id',$info['card_id']);
        unset($info['card_id']);
        $this->db->update('honda_card',$info);
    }

    public function editCode($info) {
        $this->db->where('code_id',$info['code_id']);
        unset($info['code_id']);
        $this->db->update('honda_code',$info);
    }

    public function updateCodeBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('honda_code',$update_arr,'code_number');
        }
    }

    public function updateCustomerBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('honda_customer',$update_arr,'customer_id');
        }
    }

    public function updateLotteryHistoryBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('honda_lottery_history',$update_arr,'lottery_history_id');
        }
    }
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkHasLotteryGift($customer_id){
        $this->db->from('honda_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('gift_type >',0);
        $count = $this->db->count_all_results();

        return ($count > 0);
    }



}